﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace BitcoinExchange.Data
{
    public class DataOpenOrder
    {
        public const string TABLE_NAME = "OPENORDER";
        public const string COLUMN_ORDER_ID = "ORDERID";
        public const string COLUMN_ORDER_TYPE = "ORDERTYPE";
        public const string COLUMN_BTC_PRICE = "BTCPRICE";
        public const string COLUMN_BTC_VOLUME = "BTCVOLUME";
        public const string COLUMN_BTC_OPEN_VOLUME = "BTCOPENVOLUME";
        
        public DataTable OpenOrderTable;

        public DataOpenOrder()
        {
            Initialized();
        }

        private void Initialized()
        {
            //데이터 테이블 초기화
            DataColumn col;

            OpenOrderTable = new DataTable(TABLE_NAME);

            col = OpenOrderTable.Columns.Add();
            col.ColumnName = COLUMN_ORDER_ID;
            col.DataType = System.Type.GetType("System.String");

            col = OpenOrderTable.Columns.Add();
            col.ColumnName = COLUMN_ORDER_TYPE;
            col.DataType = System.Type.GetType("System.String");

            col = OpenOrderTable.Columns.Add();
            col.ColumnName = COLUMN_BTC_PRICE;
            col.DataType = System.Type.GetType("System.Decimal");

            col = OpenOrderTable.Columns.Add();
            col.ColumnName = COLUMN_BTC_VOLUME;
            col.DataType = System.Type.GetType("System.Decimal");

            col = OpenOrderTable.Columns.Add();
            col.ColumnName = COLUMN_BTC_OPEN_VOLUME;
            col.DataType = System.Type.GetType("System.Decimal");
        }

        public void AddRow(string strOrderID, string strOrderType,  double dbBTCPrice, double dbBTCVolume, double dbBTCOpenVolume)
        {
            try
            {
                DataRow dr = OpenOrderTable.NewRow();
                dr[COLUMN_ORDER_ID] = strOrderID;
                dr[COLUMN_ORDER_TYPE] = strOrderType;
                dr[COLUMN_BTC_PRICE] = dbBTCPrice;
                dr[COLUMN_BTC_VOLUME] = dbBTCVolume;
                dr[COLUMN_BTC_OPEN_VOLUME] = dbBTCOpenVolume;
                OpenOrderTable.Rows.Add(dr);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }

        public void Clear()
        {
            OpenOrderTable.Clear();
        }

        public int RowCount()
        {
            return OpenOrderTable.Rows.Count;
        }

        public List<string> OrderIDList()
        {
            string strOrderID;

            List<string> objList = new List<string>();

            foreach (DataRow row in OpenOrderTable.Rows)
            {
                try
                {
                    strOrderID = row[COLUMN_ORDER_ID].ToString();
                    objList.Add(strOrderID);
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                }
            }

            return objList;
        }

        public Dictionary<string, string> OrderInfo(string strOrderID)
        {
            bool bfind = false;
            Dictionary<string, string> objList = new Dictionary<string, string>();


            foreach (DataRow dr in OpenOrderTable.Rows)
            {
                if (dr[COLUMN_ORDER_ID].ToString() == strOrderID)
                {
                    bfind = true;

                    objList.Add(COLUMN_ORDER_ID, dr[COLUMN_ORDER_ID].ToString());
                    objList.Add(COLUMN_ORDER_TYPE, dr[COLUMN_ORDER_TYPE].ToString());
                    objList.Add(COLUMN_BTC_PRICE, dr[COLUMN_BTC_PRICE].ToString());
                    objList.Add(COLUMN_BTC_VOLUME, dr[COLUMN_BTC_VOLUME].ToString());
                    objList.Add(COLUMN_BTC_OPEN_VOLUME, dr[COLUMN_BTC_OPEN_VOLUME].ToString());

                    break;
                }
            }

            if (!bfind)
            {
                objList.Add(COLUMN_ORDER_ID, "");
                objList.Add(COLUMN_ORDER_TYPE, "");
                objList.Add(COLUMN_BTC_PRICE, "");
                objList.Add(COLUMN_BTC_VOLUME, "");
                objList.Add(COLUMN_BTC_OPEN_VOLUME, "");
                
            }

            return objList;
        }
    }
}
